package com.lft.linked.list01.single_linked_list;

public class LinkedListDemo {
	public static void main(String[] args) {
		// 先创建节点
		// 头结点。内容为空。
		HeroNode head = new HeroNode(0, "", "");
		// 链表一所需的三个节点。
		HeroNode hero11 = new HeroNode(1, "宋江", "及时雨");
		HeroNode hero12 = new HeroNode(2, "卢俊义", "玉麒麟");
		HeroNode hero13 = new HeroNode(3, "吴用", "智多星");
		
		// 链表二所需的三个节点。
		HeroNode hero21 = new HeroNode(4, "林冲", "豹子头");
		HeroNode hero22 = new HeroNode(5, "鲁智深", "花和尚");
		HeroNode hero23 = new HeroNode(6, "武松", "行者");
		
		// 用于修改的一个节点。
		HeroNode hero17 = new HeroNode(3, "时迁", "鼓上蚤");
		
		// 用于删除的一个节点
		HeroNode hero18 = new HeroNode(4, "时迁", "鼓上蚤");
		
		System.out.println("未加节点之前——遍历链表：");
		SingleLinkedListUtil.list(head.next);
		System.out.println();
		
		head = SingleLinkedListUtil.add(head, hero13);
		head = SingleLinkedListUtil.add(head, hero11);
		head = SingleLinkedListUtil.add(head, hero12);
		head = SingleLinkedListUtil.add(head, hero18);
		
		System.out.println("加了节点之后——遍历链表：");
		SingleLinkedListUtil.list(head.next);
		
		System.out.println();
		head = SingleLinkedListUtil.update(head, hero17);
		System.out.println("修改之后——遍历链表：");
		SingleLinkedListUtil.list(head.next);
		
		System.out.println();
		head = SingleLinkedListUtil.delete(head, 4);
		System.out.println("删除之后——遍历链表：");
		SingleLinkedListUtil.list(head.next);
		
		System.out.println();
		int count = SingleLinkedListUtil.getCount(head.next);
		System.out.println("统计节点的个数：");
		System.out.println(count);
		
		System.out.println();
		HeroNode result = SingleLinkedListUtil.getHeroNodeByIndexAsc(head, 2);
		System.out.println("正序查找第 2 个点：");
		System.out.println(result);
		
		System.out.println();
		result = SingleLinkedListUtil.getHeroNodeByIndexDesc(head, 3);
		System.out.println("逆序查找第 3 个点：");
		System.out.println(result);
		
		System.out.println();
		head = SingleLinkedListUtil.reverse(head.next);
		System.out.println("反转之后——遍历链表：");
		SingleLinkedListUtil.list(head);
		
		System.out.println();
		System.out.println("逆序打印链表:");
		SingleLinkedListUtil.reverseOrderList(head);
		
		System.out.println();
		System.out.println("排序——插入排序——遍历链表：");
		head = SingleLinkedListUtil.insertionSortList(head);
		SingleLinkedListUtil.list(head);
		
		System.out.println();
		System.out.println("新建一个新节点：");
		HeroNode head2 = new HeroNode(0, "", "");
		head2 = SingleLinkedListUtil.addByOrder(head2, hero23);
		head2 = SingleLinkedListUtil.addByOrder(head2, hero22);
		head2 = SingleLinkedListUtil.addByOrder(head2, hero21);
		SingleLinkedListUtil.list(head2.next);
		
		// System.out.println();
		// System.out.println("合并之后——遍历链表：");
		// HeroNode mergeList = SingleLinkedListUtil.mergeList(head, head2.next);
		// SingleLinkedListUtil.list(mergeList);
		
		System.out.println();
		System.out.println("合并之后——遍历链表：");
		HeroNode mergeList = SingleLinkedListUtil.mergeToOrderList(head, head2.next);
		SingleLinkedListUtil.list(mergeList);
		
		System.out.println();
		System.out.println("反转2-4之后——遍历链表：");
		
		Runtime r = Runtime.getRuntime();
		r.gc();// 计算内 存前先垃圾回zhi 收一次
		long start = System.currentTimeMillis();// 开始 Time
		long startMem = r.freeMemory(); // 开始 Memory
		mergeList = SingleLinkedListUtil.reverseBetween(mergeList, 2, 4);
		long endMem = r.freeMemory(); // 末尾 Memory
		long end = System.currentTimeMillis();// 末尾 Time
		SingleLinkedListUtil.list(mergeList);
		// 输出
		System.out.println("TimeCost: " + (end - start) * 1000 + "ms");
		System.out.println("MemoryCost: " + (startMem - endMem) + "KB");
		
		System.out.println();
		System.out.println("反转2-4之后——遍历链表：");
		
		Runtime r2 = Runtime.getRuntime();
		r2.gc();// 计算内 存前先垃圾回zhi 收一次
		long start1 = System.currentTimeMillis();// 开始 Time
		long startMem1 = r2.freeMemory(); // 开始 Memory
		mergeList = SingleLinkedListUtil.recursionReverseBetween(mergeList, 2, 4);
		long endMem1 = r2.freeMemory(); // 末尾 Memory
		long end1 = System.currentTimeMillis();// 末尾 Time
		SingleLinkedListUtil.list(mergeList);
		// 输出
		System.out.println("TimeCost: " + (end1 - start1) * 1000 + "ms");
		System.out.println("MemoryCost: " + (startMem1 - endMem1) + "KB");
		
		//
		// System.out.println();
		// System.out.println("合并之后再排序一次：");
		// HeroNode heroNode = SingleLinkedListUtil.recursionInsertionSortList(mergeList);
		// SingleLinkedListUtil.list(heroNode);
	}
}
